export default {
    // action用于存放异步操作 -> 在异步操作结束之后,再掉头mutations中的方法修改数据
    // context  => 和store实例用相同属性和方法的对象 (新对象)
    // payload  => actions中的方法被调用时,也可以接收参数



    // 简单写法 => 在异步操作结束之后,再调用mutations中的方法修改数据 (不能知道异步操作什么时候结束)
    addA_Async: function (context, payload) {
      // context =>  {state,getters,commit,dispatch}
      console.log("context", context);
      console.log("payload", payload);

      var { commit } = context;
      setTimeout(() => {
        commit("addA", payload);
      }, 1000);
    },
    addB_Async: function (context, payload) {
      // context =>  {state,getters,commit,dispatch}
      console.log("context", context);
      console.log("payload", payload);

      var { commit } = context;
      setTimeout(() => {
        commit("addB", payload);
      }, 1000);
    },

    // Promise改写版本 =>  返回一个Promise实例(pending)  -> 在异步操作结束之后,再调用mutations中的方法修改数据(将Promise实例的状态改为fulfilled)
    addA_Async_Pro: function (context, payload) {
      // context =>  {state,getters,commit,dispatch}

      return new Promise(function (resolve) {
        console.log("context", context);
        console.log("payload", payload);
        var { commit } = context;
        setTimeout(() => {
          commit("addA", payload);
          resolve(true);
        }, 1000);
      })
    },
    addB_Async_Pro: function (context, payload) {
      // context =>  {state,getters,commit,dispatch}

      return new Promise(function (resolve) {
        console.log("context", context);
        console.log("payload", payload);
        var { commit } = context;
        setTimeout(() => {
          commit("addB", payload);
          resolve(true);
        }, 1000);
      })
    },

    // action中复用mutations中的多个方法
    addBoth: function (context, payload) {
      // context =>  {state,getters,commit,dispatch}
      console.log("参数", payload);
      var { commit } = context;
      commit("addA", payload.a);
      commit("addB", payload.b);
    },

    // action中复用actions中的多个方法
    addBoth_Async: function (context, payload) {
      // context =>  {state,getters,commit,dispatch}
      console.log("参数", payload);
      var { dispatch } = context;
      dispatch("addA_Async", payload.a);
      dispatch("addB_Async", payload.b);
    },

    addBoth_Async_Pro: async function (context, payload) {
      // context =>  {state,getters,commit,dispatch}
      console.log("参数", payload);
      var { dispatch } = context;
      await dispatch("addA_Async_Pro", payload.a)
      await dispatch("addB_Async_Pro", payload.b);
      alert("A和B更新完毕")
    }


  }